/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package HumanCentredSystemDesign;
import java.util.*;

/**
 *
 * @author Chris and Dom
 */
public class RenewManager extends LibraryCirculations {

    
    public void renew(String dewey){
        try{
            //Correction mb - use the ISBN/ISSN associated with the dewey
            connect1.establishConnection();
            // Get Item from Dewey Index
            myReservations = connect1.getReservationsFromDatabase(this.getCopyFromDewey(dewey).getItemIdentifier(),"");
            connect1.closeConnection();
            }
            catch(Exception err){
                String errString = "Probelems getting reservation: " + err.toString();
                AlertWindow aw = new AlertWindow(errString);
                aw.setVisible(true);
                System.out.println(errString);
                return;
            }        
        try{
             myLoan = getLoanFromDewey(dewey); // Get copy details from copyDewey
        }
        catch(Exception err){
            String errString = "Dewey does not exist in the database: " + err.toString();
            AlertWindow aw = new AlertWindow(errString);
            aw.setVisible(true);
            System.out.println(errString);
            return;
        }

        try{
            myBorrower = getBorrower(myLoan.getLibraryMemberID(), "", "", "").get(0); // Get copy details from copyDewey
        }
        catch(Exception err){
            String errString = "Borrower does not exist " + err.toString();
            AlertWindow aw = new AlertWindow(errString);
            aw.setVisible(true);
            System.out.println(errString);
            return;
        }
        
        ArrayList <String[]> overdueLoans;
        try{
            overdueLoans = overdue(myBorrower);
            }
            catch(Exception err){
                String errString = "Problems retrieving overdue loans: " + err.toString();
                AlertWindow aw = new AlertWindow(errString);
                aw.setVisible(true);
                System.out.println(errString);
                return;
            }
        Date d = new Date();
       
        
        if(!myReservations.isEmpty())
        { 
                AlertWindow aw = new AlertWindow("Cannot renew item is reserved");
                aw.setVisible(true);
                return;
        }
        else if (!overdueLoans.isEmpty())
        {
                AlertWindow aw = new AlertWindow("Cannot renew you have overdue loans");
                aw.setVisible(true);
                return;
        }
        else 
        {
            try{
                myLoan.removeLoanFromDatabase();
                
            } catch(Exception err){
                String errString = "Could not remove loan from database: " + err.toString();
                AlertWindow aw = new AlertWindow(errString);
                aw.setVisible(true);
                System.out.println(errString);
                return;
            }
            Loan loan = new Loan(d, dewey, myBorrower.getBorrowerID(), 21);
            try{
               loan.saveToDatabase();
            } catch(Exception err){
                String errString = "Could not save loan from database: " + err.toString();
                AlertWindow aw = new AlertWindow(errString);
                aw.setVisible(true);
                System.out.println(errString);
                return; 
            }
        }
    }


 
    
}

